package com.probiz.estore.product.dao.impl;

import java.math.BigDecimal;
import java.util.List;

import org.hibernate.Query;

import com.probiz.estore.common.model.product.Brand;
import com.probiz.estore.common.model.product.ProductType;
import com.probiz.estore.common.model.product.SkuOption;
import com.probiz.estore.core.dao.impl.HibernateGenericDaoImpl;
import com.probiz.estore.product.dao.ProductTypeDao;

/**
 * Dao implementation for ProductType.
*/
public class ProductTypeDaoImpl extends HibernateGenericDaoImpl<ProductType> implements ProductTypeDao {

	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.catalog.dao.ProductTypeDao#getMaximumPriceByProductType(java.lang.Integer)
	 */
	public int getMaximumPriceByProductType(Integer productTypeId) {
		StringBuilder hql = new StringBuilder();
		hql.append("select max(ps.salePrice), max(ps.price), max(ps.listPrice) ")
			.append("from ProductSku ps, Product p ")
			.append("where 1=1 ")
			.append("	and p.productId = ps.product.productId ")
			.append("	and p.status = 1 ")
			.append("	and p.productType.productTypeId = ?");
		
		List list = getSession(false).createQuery(hql.toString()).setInteger(0, productTypeId).list();
		if (list != null && !list.isEmpty() && list.get(0) != null) {
			Object[] o = (Object[])list.get(0);
			int price0 = 0, price1 = 0, price2 = 0;
			if (o[0] != null) {
				price0 =  ((BigDecimal)o[0]).intValue();
			}
			if (o[1] != null) {
				price1 =  ((BigDecimal)o[1]).intValue();
			}
			if (o[2] != null) {
				price2 =  ((BigDecimal)o[2]).intValue();
			}
			
			if (price1 >= price0 && price1 >= price2) {
				return price1;
			} else 
			if (price2 >= price1 && price2 >= price0) {
				return price2;
			} else {
				return price0;
			}
		}
		
		return 0;
	}
	
	 /*
	  * (non-Javadoc)
	  * @see com.probiz.estore.product.dao.ProductTypeDao#getRelatedBrand(java.lang.Integer)
	  */
    public List<Brand> getRelatedBrand(Integer productTypeId){
    	return this.findByHql("select pb.brand from ProductTypeBrand pb where pb.productType.productTypeId= ? order by pb.sortOrder", productTypeId);
    }
   
    public List<SkuOption> getRelatedSkuOption(Integer productTypeId){
    	return this.findByHql("select ps.skuOption from ProductTypeSkuOption ps where ps.productType.productTypeId= ? order by ps.sortOrder", productTypeId);
    	
    }
    public ProductType getProductTypeByName(String productTypeName){
    	List<ProductType> productTypeList = this.findByHql("from ProudctType pt where pt.productTypeName =?",productTypeName);
    	if(productTypeList.size()>0){
    		return productTypeList.get(0);
    	}else{
    		return null;
    	}
    }

    public Long getProductCountByProductType(Integer productTypeId){
    	return (Long)this.findUnique("select count(p.productId) from Product p where p.productType.productTypeId =?",productTypeId);
    }
    
    public boolean isProductTypeExist(String productTypeName) {
        Long typeCount = this.countByHql("from ProductType where productTypeName = ?", productTypeName);
        if (typeCount != null && typeCount==0) {
            return false;
        } else {
            return true;
        }
    }
	
	public boolean isProductTypeNameUseByOthers(String productTypeName, Integer productTypeId) {
        String hql = "select count(*) from ProductType where productTypeName =:productTypeName and productTypeId<>:productTypeId";
        Query query = getSession().createQuery(hql);
        query.setString("productTypeName", productTypeName);
        query.setInteger("productTypeId", productTypeId.intValue());
        int countNumber = ((Long) query.uniqueResult()).intValue();
        if (countNumber > 0) {
            return true;
        } else {
            return false;
        }
    }
}
